//
//  XJUserAccount.swift
//  新浪微博
//
//  Created by 徐绩 on 16/4/19.
//  Copyright © 2016年 ayumi. All rights reserved.
//

import UIKit

class XJUserAccount: NSObject, NSCoding {
    /*
     看文档接口数据
    "access_token": "ACCESS_TOKEN",
    "expires_in": 1234,
    "remind_in":"798114",
    "uid":"12341234"
    
    */
    var access_token: String?
    var remind_in: String?
    var uid: String?
    var avatar_large: String?
    var name: String?
    
    //根据生命周期计算过期时间
    var expires_in: NSTimeInterval = 0 {
    
        didSet{
        
            //过期时间:从token取回来的那一刻起,expires_in秒后
        expiresDate = NSDate(timeIntervalSinceNow: expires_in)
        }
    }
    var expiresDate: NSDate?
    
    init(dict: [String: AnyObject]) {
     
        super.init()
        setValuesForKeysWithDictionary(dict)
    }
    
    //解档
    required init?(coder aDecoder: NSCoder) {
    
        access_token = aDecoder.decodeObjectForKey("access_token") as? String
        expires_in = aDecoder.decodeDoubleForKey("expires_in")
        remind_in = aDecoder.decodeObjectForKey("remind_in") as? String
        uid = aDecoder.decodeObjectForKey("uid") as? String
        avatar_large = aDecoder.decodeObjectForKey("avatar_large") as? String
        name = aDecoder.decodeObjectForKey("name") as? String
        expiresDate = aDecoder.decodeObjectForKey("expiresDate") as? NSDate
        
        
    
    }
    
    //归档
    func encodeWithCoder(aCoder: NSCoder) {
    
        aCoder.encodeObject(access_token,forKey: "access_token")
        aCoder.encodeDouble(expires_in, forKey: "expires_in")
        aCoder.encodeObject(remind_in,forKey: "remind_in")
        aCoder.encodeObject(uid,forKey: "uid")
        aCoder.encodeObject(avatar_large,forKey: "avatar_large")
        aCoder.encodeObject(name,forKey: "name")
        aCoder.encodeObject(expiresDate,forKey: "expiresDate")
    }

}
